package com.googlecode.c4e.internal;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.model.WorkbenchContentProvider;

import com.googlecode.c4e.model.Profile;
import com.googlecode.c4e.model.Project;
import com.googlecode.c4e.model.ProjectGroup;
import com.googlecode.c4e.model.RootElement;
import com.googlecode.c4e.ui.adapters.ProfileAdapter;
import com.googlecode.c4e.ui.adapters.ProjectAdapter;
import com.googlecode.c4e.ui.adapters.ProjectGroupAdapter;
import com.googlecode.c4e.ui.adapters.RootElementAdapter;

public class ContentProvider extends WorkbenchContentProvider implements IAdapterFactory{

  @SuppressWarnings("unchecked")
  private Map<Class, IWorkbenchAdapter> adapters;

  public ContentProvider() {
    adapters = new HashMap<Class, IWorkbenchAdapter>();
    adapters.put(Profile.class, new ProfileAdapter());
    adapters.put(RootElement.class, new RootElementAdapter());
    adapters.put(ProjectGroup.class, new ProjectGroupAdapter());
    adapters.put(Project.class, new ProjectAdapter());
  }

  @Override
  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    super.inputChanged(viewer, oldInput, newInput);
  }

  @Override
  protected void processDelta(IResourceDelta delta) {
    super.processDelta(delta);
  }

  @Override
  protected IWorkbenchAdapter getAdapter(Object element) {
    IWorkbenchAdapter result = adapters.get(element.getClass());
    if (result != null) {
      return result;
    } else {
      return super.getAdapter(element);
    }
  }

  @Override
  public Object[] getChildren(Object element) {
    return super.getChildren(element);
  }

  @Override
  public Object[] getElements(Object element) {
    return super.getElements(element);
  }

  @Override
  public Object getParent(Object element) {
    return super.getParent(element);
  }

  @Override
  public boolean hasChildren(Object element) {
    if (element instanceof Project) {
      return false;
    }
    try {
      return super.hasChildren(element);
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }

  @SuppressWarnings("unchecked")
  public Object getAdapter(Object obj, Class class1) {
    return getAdapter(obj);
  }

  @SuppressWarnings("unchecked")
  public Class[] getAdapterList() {
    return new Class[] {IWorkbenchAdapter.class};
  }
}
